package com.solution.dao;

import java.util.List;

import org.apache.log4j.Logger;
import org.hibernate.HibernateException;
import org.hibernate.Query;

import com.solution.exception.DAOException;
import com.solution.model.PO;
import com.solution.model.UserPermissionType;
/**
 * Implementation for the CRUD functionalities of user permission type.
 *
 */
public class UserPermissionTypeDAOImpl extends DAOImpl<UserPermissionType> implements UserPermissionTypeDAO{

	/**
	 * Logger static object.
	 */
	private Logger log = Logger.getLogger(UserPermissionTypeDAOImpl.class.getName());
	
	@Override
	protected Class<? extends PO> getPOClass() {		
		return UserPermissionType.class;
	}	
	
	/**
	 * get list of permissions for user
	 * @param userID
	 * @return
	 * @throws DAOException
	 */
	public List<UserPermissionType> getPermissionListByUserId(Long userID) throws DAOException {
		log.debug("Entering :: UserPermissionTypeDAOImpl:getPermissionListByUserId()");
		try{
			String hql = "from UserPermissionType u where u.id in (select id.userPermissionTypeId from UserRolePermission where userRoleType.id in ( select userRoleType.id from UserRole where userInfo.id =:userID))";
			//String hql = "select u.id,u.name,u.groupName, u.description ,u.status ,u.createdOn,u.createdBy from UserPermissionType u where u.id in (select id.userPermissionTypeId from UserRolePermission where userRoleType.id in ( select userRoleType.id from UserRole where userInfo.id =:userID)) order BY u.id,u.groupName";
			Query query = getCurrentSession().createQuery(hql);
	        query.setLong("userID",userID);                
	        List <UserPermissionType> list =query.list();
	        return list;
		}
		catch(HibernateException he){
			log.error(he, he);
			throw new DAOException(he);
		}
	}
}
